Feature/add language option to weather agent (#1544)

* Add language parameter to the weather agent

* Add text forecast for Wunderground data provider

Wunderground API returns forecast for the next few days. Returned JSON response has two types of data under the "forecast" key:
- "txt_forecast": pre-formatted text representation
- "simpleforecast": raw data representation

The "period" key contains which day the node belongs to.

Currently only "simpleforecast" is available to users of the Weather agent.
This commit appends the "txt_forecast" representation to the current data set.

Géza Búza лет %!s(int64=8): %!d(string=назад)
Родитель
Сommit
c3ab764efc
1 измененных файлов с 17 добавлено и 4 удалено
  1. 17 4
      app/models/agents/weather_agent.rb

+ 17 - 4
app/models/agents/weather_agent.rb

@@ -25,6 +25,8 @@ module Agents
25 25
       You must setup an [API key for Forecast](https://developer.forecast.io/) in order to use this Agent with ForecastIO.
26 26
 
27 27
       Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
28
+
29
+      If you want to see the returned texts in your language, then set the `language` parameter in ISO 639-1 format.
28 30
     MD
29 31
 
30 32
     event_description <<-MD
@@ -68,10 +70,11 @@ module Agents
68 70
         'api_key' => 'your-key',
69 71
         'location' => '94103',
70 72
         'which_day' => '1',
73
+        'language' => 'EN',
71 74
         'expected_update_period_in_days' => '2'
72 75
       }
73 76
     end
74
-    
77
+
75 78
     def check
76 79
       if key_setup?
77 80
         create_event :payload => model(weather_provider, which_day).merge('location' => location)
@@ -79,7 +82,7 @@ module Agents
79 82
     end
80 83
 
81 84
     private
82
-    
85
+
83 86
     def weather_provider
84 87
       interpolated["service"].presence || "wunderground"
85 88
     end
@@ -92,6 +95,10 @@ module Agents
92 95
       interpolated["location"].presence || interpolated["zipcode"]
93 96
     end
94 97
 
98
+    def language
99
+      interpolated['language'].presence || 'EN'
100
+    end
101
+
95 102
     def validate_options
96 103
       errors.add(:base, "service must be set to 'forecastio' or 'wunderground'") unless ["forecastio", "wunderground"].include?(weather_provider)
97 104
       errors.add(:base, "location is required") unless location.present?
@@ -100,14 +107,20 @@ module Agents
100 107
     end
101 108
 
102 109
     def wunderground
103
-      Wunderground.new(interpolated['api_key']).forecast_for(location)['forecast']['simpleforecast']['forecastday'] if key_setup?
110
+      if key_setup?
111
+        forecast = Wunderground.new(interpolated['api_key'], language: language.upcase).forecast_for(location)
112
+        merged = {}
113
+        forecast['forecast']['simpleforecast']['forecastday'].each { |daily| merged[daily['period']] = daily }
114
+        forecast['forecast']['txt_forecast']['forecastday'].each { |daily| (merged[daily['period']] || {}).merge!(daily) }
115
+        merged
116
+      end
104 117
     end
105 118
 
106 119
     def forecastio
107 120
       if key_setup?
108 121
         ForecastIO.api_key = interpolated['api_key']
109 122
         lat, lng = location.split(',')
110
-        ForecastIO.forecast(lat,lng)['daily']['data']
123
+        ForecastIO.forecast(lat, lng, params: {lang: language.downcase})['daily']['data']
111 124
       end
112 125
     end
113 126